<?php
include_once('./config/config.php');

// 获取部门id
$id = $_GET['id'];
// 查询该id的部门名称
$sql = "select * from `pre_department` where `id`= '$id'";
// 执行
$Dep = find($sql);

if ($_POST) {
  $name = $_POST["name"];

  if (empty($name)) {
    ShowMsg('请输入部门名称');
  }
  // 封装更新数据
  $data = [
    'name' => $name,
  ];
  
  $result = update('department', $data, "`id` = '$id'");
  // 判断是否修改成功
  if ($result['code'] === 1) {
    ShowMsg($result['msg'], 'DepList.php');
  } else {
    echo $result['msg'];
    exit;
  }
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入公共样式 -->
  <?php include_once('./common/meta.php'); ?>

  <!-- 引入公共js -->
  <?php include_once('./common/script.php'); ?>
</head>
<style>
  input[name="name"] {
    margin-bottom: 0px;
  }
</style>

<body>
  <!-- 引入头部 -->
  <?php include_once('./common/header.php'); ?>

  <!-- 引入侧边菜单 -->
  <?php include_once('./common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">编辑部门</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">Index</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='DepList.php'"><i class="icon-list"></i> 部门列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>部门名称</label>
                <input type="text" name="name" id="name" class="input-xxlarge" value="<?php echo $Dep['name'] ?>">
                <input class="btn btn-primary" type="submit" value="修改" />
              </form>
            </div>
          </div>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Delete Confirmation</h3>
          </div>
          <div class="modal-body">

            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?
            </p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php'); ?>
      </div>
    </div>
  </div>

</body>

</html>

<script>
  $('#avatar').change(function() {
    var file = $(this)[0].files[0]

    var url = GetObjectURL(file)

    $('#img').attr('src', url)
  })


  $('#name').change(function() {
    var name = $(this).val();
    $.ajax({
      // 提交方式
      type: 'POST',
      // 请求地址
      url: 'proces.php',
      data: {
        // 用于判断什么请求
        action: 'DepSelect',
        name,
      },
      // 请求返回的数据类型 text html json
      dataType: 'json',
      success: function(data) {
        if (data.code === 1) {
          layer.msg(data.msg);
        }
      }
    })
  })
</script>